package cn.bonoon.kernel.support.services.handler;

import javax.persistence.EntityManager;

import cn.bonoon.kernel.events.OperateEvent;
import cn.bonoon.kernel.support.entities.EntityStatus;
import cn.bonoon.kernel.support.entities.Persistable;

public class OperateStatusHandler implements OperateHandler{
	
//	private final String updateStatus;
	private final int status;
	
	public OperateStatusHandler(int status){
		this.status = status;
//		this.updateStatus = "update " + entityName + " set status=" + status + " where ";
	}
	
	@Override
	public Object handler(EntityManager entityManager, OperateEvent event, Persistable entity) {
		EntityStatus source = (EntityStatus)entity;
		source.setStatus(status);
		entityManager.merge(source);
		return "实体" + source.getId() + "状态值改变为" + status;
	}
	
	@Override
	public MultiOperateHandlerExecuter handler(final EntityManager entityManager, final OperateEvent event) {
		return new MultiOperateHandlerExecuter(){

			@Override
			public MultiOperateHandlerExecuter addParameter(Persistable entity) {
				EntityStatus source = (EntityStatus)entity;
				source.setStatus(status);
				entityManager.merge(source);
				return this;
			}

			@Override
			public void execute() { }
			
		};
//		StringBuilder ql = new StringBuilder(updateStatus);
//		ql.append("id in(").append(ids[0]);
//		String sids = "实体" + ids[0];
//		for(int i = 1; i < ids.length; i++){
//			ql.append(',').append(ids[i]);
//			sids += ',' + ids[i];
//		}
//		ql.append(')');
//		service.getEntityManager().createQuery(ql.toString()).executeUpdate();
//		return sids + "状态值改变为" + status;
	}
}
